package com.mall.order.services;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mall.order.OrderQueryService;
import com.mall.order.biz.convert.Do2DtoConvert;
import com.mall.order.constant.OrderRetCode;
import com.mall.order.dal.entitys.Order;
import com.mall.order.dal.entitys.OrderItem;
import com.mall.order.dal.entitys.OrderShipping;
import com.mall.order.dal.persistence.OrderItemMapper;
import com.mall.order.dal.persistence.OrderMapper;
import com.mall.order.dal.persistence.OrderShippingMapper;
import com.mall.order.dto.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.Example;

import java.math.BigDecimal;
import java.util.List;

/**
 *  ciggar
 * create-date: 2019/7/30-上午10:04
 */
@Slf4j
@Component
@Service
public class OrderQueryServiceImpl implements OrderQueryService {

    @Autowired
    OrderMapper orderMapper;

    @Autowired
    OrderItemMapper orderItemMapper;

    @Autowired
    OrderShippingMapper orderShippingMapper;

    @Autowired
    Do2DtoConvert do2DtoConvert;

    @Override
    public OrderListResponse queryOrders(OrderListRequest request) {

        OrderListResponse orderListResponse = new OrderListResponse();

        try {
            request.requestCheck();
            Integer page = request.getPage();
            Integer size = request.getSize();
            PageHelper.startPage(page, size);
            Example example = new Example(Order.class);
            example.setOrderByClause("create_time desc");
            example.createCriteria().andEqualTo("userId", request.getUserId());
            List<Order> orders = orderMapper.selectByExample(example);
            PageInfo<Order> pageInfo = new PageInfo<>(orders);

            List<OrderDetailInfo> orderDetailInfos = do2DtoConvert.orders2OrderDetailInfos(orders);

            for (OrderDetailInfo orderDetailInfo : orderDetailInfos) {
                String orderId = orderDetailInfo.getOrderId();
                // orderItemDto orderShippingDto还没有查出来
                Example orderItemMapperExample = new Example(OrderItem.class);
                orderItemMapperExample.createCriteria().andEqualTo("orderId", orderId);
                List<OrderItem> orderItems = orderItemMapper.selectByExample(orderItemMapperExample);
                List<OrderItemDto> orderItemDtos = do2DtoConvert.orderItems2OrderItemDtos(orderItems);

                orderDetailInfo.setOrderItemDto(orderItemDtos);
                // 地址信息
                OrderShipping orderShipping = orderShippingMapper.selectByPrimaryKey(orderId);
                OrderShippingDto orderShippingDto = do2DtoConvert.orderShipping2OrderShippingDto(orderShipping);
                orderDetailInfo.setOrderShippingDto(orderShippingDto);
            }

            orderListResponse.setDetailInfoList(orderDetailInfos);
            orderListResponse.setTotal(pageInfo.getTotal());
            orderListResponse.setCode(OrderRetCode.SUCCESS.getCode());
            orderListResponse.setMsg(OrderRetCode.SUCCESS.getMessage());
        } catch (Exception e) {
            orderListResponse.setCode(OrderRetCode.ORDER_SELECT_FAILED.getCode());
            orderListResponse.setMsg(OrderRetCode.ORDER_SELECT_FAILED.getMessage());
        }

        return orderListResponse;
    }

    @Override
    public OrderResponse queryOrder(OrderRequest request) {

        OrderResponse orderResponse = new OrderResponse();

        try {
            request.requestCheck();
            String orderId = request.getOrderId();
            Order order = orderMapper.selectByPrimaryKey(orderId);
            OneOrderDto oneOrderDto = do2DtoConvert.order2OneOrderDto(order);

            // 还差goodsList streetName tel userName
            OrderShipping orderShipping = orderShippingMapper.selectByPrimaryKey(orderId);
            oneOrderDto.setTel(orderShipping.getReceiverPhone());
            oneOrderDto.setUserName(orderShipping.getReceiverName());


            String state = orderShipping.getReceiverState();
            String city = orderShipping.getReceiverCity();
            String district = orderShipping.getReceiverDistrict();
            String address = orderShipping.getReceiverAddress();
            StringBuilder sb = new StringBuilder();
            sb.append(state)
                    .append("-")
                    .append(city)
                    .append("-")
                    .append(district)
                    .append("-")
                    .append(address);
            oneOrderDto.setStreetName(sb.toString());

            //还差goodsList
            Example example = new Example(OrderItem.class);
            example.createCriteria().andEqualTo("orderId", orderId);
            List<OrderItem> orderItems = orderItemMapper.selectByExample(example);
            List<OrderItemDto> goodsList = do2DtoConvert.orderItems2OrderItemDtos(orderItems);
            oneOrderDto.setGoodsList(goodsList);

            // 算总价
            BigDecimal total = new BigDecimal(0);
            for (OrderItem orderItem : orderItems) {
                Integer num = orderItem.getNum();
                Double price = orderItem.getPrice();
                total = total.add(new BigDecimal(num * price));
            }
            oneOrderDto.setOrderTotal(total);

            orderResponse.setOrderDto(oneOrderDto);
            orderResponse.setCode(OrderRetCode.SUCCESS.getCode());
            orderResponse.setMsg(OrderRetCode.SUCCESS.getMessage());
        } catch (Exception e) {
            orderResponse.setCode(OrderRetCode.ORDER_SELECT_FAILED.getCode());
            orderResponse.setMsg(OrderRetCode.ORDER_SELECT_FAILED.getMessage());
        }


        return orderResponse;
    }
}
